/*
 * Sdl.c
 *
 *  Created on: 12 nov. 2011
 *      Author: alex
 */
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <limits.h>
#include "Sudoku.h"

SDL_Surface* initSDL(void){
	SDL_Surface *ecran = NULL;
  if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    fprintf(stderr, "Erreur à l'initialisation de la SDL : %s\n", SDL_GetError());
    exit(EXIT_FAILURE);
  }

  atexit(SDL_Quit);
  ecran = SDL_SetVideoMode(40*10, 40*9, 32, SDL_SWSURFACE);

  if ( ecran == NULL) {
    fprintf(stderr, "Impossible d'activer le mode graphique : %s\n", SDL_GetError());
    exit(EXIT_FAILURE);
  }

  SDL_WM_SetCaption("Mon premier programme SDL", NULL);
  return ecran;
}

void init_cases( SDL_Surface ***gris, SDL_Surface ***blanc, SDL_Surface ***vert_claire, SDL_Surface ***vert_fonce, SDL_Surface ***marron ){
	int i;
	char buffer[PATH_MAX];
	*gris = (SDL_Surface**) malloc( sizeof(SDL_Surface*) * 10);
	*blanc = (SDL_Surface**) malloc( sizeof(SDL_Surface*) * 10);
	*vert_fonce = (SDL_Surface**) malloc( sizeof(SDL_Surface*) * 10);
	*vert_claire = (SDL_Surface**) malloc( sizeof(SDL_Surface*) * 10);
	*marron = (SDL_Surface**) malloc( sizeof(SDL_Surface*) * 10);

	for( i = 0; i < 10; i++ ){
		sprintf(buffer, "svn/trunk/images/%d_blanc.png", i);
		(*blanc)[i] = IMG_Load( buffer );
		sprintf(buffer, "svn/trunk/images/%d_gris.png", i);
		(*gris)[i] = IMG_Load( buffer );
		sprintf(buffer, "svn/trunk/images/%d_vert.png", i);
		(*vert_claire)[i] = IMG_Load( buffer );
		sprintf(buffer, "svn/trunk/images/%d_vert_fonce.png", i);
		(*vert_fonce)[i] = IMG_Load( buffer );
		sprintf(buffer,"svn/trunk/images/%d_marron.png",i);
		(*marron)[i] = IMG_Load( buffer );
	}
}

void dessine_grille( SDL_Surface *ecran, const SDL_Surface **gris, const SDL_Surface *marron, Sudoku_s *grille ){
	int i,j;
	SDL_Rect pos;

	for( i = 0; i < grille->hauteur; i++ ){
		for( j = 0; j < grille->largeur; j++ ){
			pos.x = j*40;
			pos.y = i*40;
			SDL_BlitSurface( gris[grille->tab[i][j]], NULL, ecran, &pos );
		}
	}
	for( i = 0; i < 9; i++){
		pos.x = 360;
		pos.y = 40*i;
		SDL_BlitSurface( marron, NULL, ecran, &pos);
	}

	SDL_Flip( ecran );
}

void peint_lettre( SDL_Surface *ecran, SDL_Surface **cases ){
	SDL_Rect rect;
	int i;

	for( i = 0; i < 9; i++ ){
		rect.x = 360;
		rect.y = 40 * (i);
		SDL_BlitSurface( cases[i+1], NULL, ecran, &rect);
	}
}

void colorie_grille( SDL_Surface *ecran, SDL_Surface **vert, SDL_Surface **marron, Sudoku_s *grille, const SDL_Rect const *pos){
	int i, j,debutx,debuty;
	SDL_Rect tmp;

	for( i = 0; i < 9; i++ ){
		tmp.x = i*40;
		tmp.y = 40 * (pos->y/40);
		if( i != pos->x/40 )
			SDL_BlitSurface( vert[grille->tab[pos->y/40][i]], NULL, ecran, &tmp);
	}

	for( i = 0; i < 9; i++ ){
		tmp.x = 40 * (pos->x/40);
		tmp.y = i*40;
		if( i != pos->y/40 )
			SDL_BlitSurface( vert[grille->tab[i][pos->x/40]], NULL, ecran, &tmp);
	}

	debutx = ((pos->x/40)/3)*3;
	debuty = ((pos->y/40)/3)*3;

	for( i = debuty; i < debuty+3; i++){
		for( j = debutx; j < debutx+3; j++ ){
			tmp.y = i * 40;
			tmp.x = j * 40;
			if( i != pos->y/40 && j != pos->x /40 )
				SDL_BlitSurface( vert[grille->tab[i][j]], NULL, ecran, &tmp);
		}
	}
	SDL_Flip( ecran );
}

void test_img( SDL_Surface *ecran, SDL_Surface **gris ){
	int i;
	SDL_Rect pos;

	for( i = 0; i < 9; i++ ){
		pos.x = i*40;
		pos.y = 0;
		SDL_BlitSurface( gris[i], NULL, ecran, &pos );
	}
	SDL_Flip( ecran );
}

void remove_droite( SDL_Surface *ecran, const SDL_Surface const *rideau ){
	SDL_Rect pos;
	int i;
	for( i = 0; i < 9; i++ ){
		pos.x = 360;
		pos.y = i*40;
		SDL_BlitSurface( rideau, NULL, ecran, &pos);
	}
}

void place_nombre( SDL_Surface *ecran, SDL_Surface **gris, SDL_Rect *position, const int number ){
	SDL_BlitSurface( gris[number], NULL, ecran, position);
	SDL_Flip( ecran );
}

void remove_color( SDL_Surface *ecran, SDL_Surface **gris, Sudoku_s *grille ){
	int i,j;
	SDL_Rect pos;

	for( i = 0; i < grille->hauteur; i++ ){
		for( j = 0; j < grille->largeur; j++ ){
			pos.y = i*40;
			pos.x = j*40;
			SDL_BlitSurface( gris[grille->tab[i][j]], NULL, ecran, &pos);
		}
	}
}
